package com.schooleducation.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.schooleducation.entity.Lesson;
import com.schooleducation.entity.LessonSubscribe;
import com.schooleducation.entity.LessonTwoType;
import com.schooleducation.entity.User;
import com.schooleducation.service.ServiceFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping("/onlineLesson")
public class OnlineLessonController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;
	
	private int id = 0;

	/**
	 * 获取在线课程页面
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String getPage(HttpSession session, Model model) {
		model.addAttribute("lessonType", serviceFac.getLessonService().getLessonTypeAll());
		List<Lesson> lessons = serviceFac.getLessonService().getLessonList(0);
		model.addAttribute("user", session.getAttribute("user"));
		model.addAttribute("lesson", lessons);

		return "onlineLesson";
	}
	
	/**
	 * 按课程类别进行分类
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getLessonListByType(int typeId) {
		id = typeId;
		Lesson lesson = new Lesson();
		LessonTwoType ltt = new LessonTwoType();
		ltt.setId(typeId);
		lesson.setLessonType(ltt);
		List<Lesson> lessons = serviceFac.getLessonService().getLessonListByType(lesson, 0);
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[]{"lessonType","lessonInfo","teacher"});
		return JSONArray.fromObject(lessons, config).toString();
	}
	
	/**
	 * 翻页
	 * @return
	 */
	public String getLessonListByPage() {
		if(id == 0){
			
		}else{
			
		}
		
		return "";
	}


	/**
	 * 订阅课程
	 * @param session
	 * @param lessonId
	 * @return
	 */
	@RequestMapping(value = "subscribe", method = RequestMethod.POST)
	@ResponseBody
	public String subscribeLesson(HttpSession session, int lessonId) {
		Map<String, Object> state = new HashMap<String, Object>();
		
		User user = (User) session.getAttribute("user");
		if (user == null) {
			state.put("isLogin", "no");
		} else {
			state.put("isLogin", "yes");
			Lesson lesson = new Lesson();
			lesson.setId(lessonId);
			LessonSubscribe lessonSubscribe = new LessonSubscribe();
			lessonSubscribe.setUser(user);
			lessonSubscribe.setLesson(lesson);
			if (serviceFac.getLessonService().addSubscribeLesson(lessonSubscribe)) {
				state.put("deal", "success");
			} else {
				state.put("deal", "fail");
			}
		}

		return JSONObject.fromObject(state).toString();
	}
}
